package com.iplatform.module.auth;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;

/**
 * 认证中心userDetails外观类
 * @author red
 */
public class AuthUserDetailsFacade implements Serializable, UserDetails {

    /**
     * 实际的userDetails对象
     */
    private JSONObject userDetails;

    /**
     * 实际的userDetails对象，对应用户中心serviceId
     */
    private String userServiceId;


    private Collection<? extends GrantedAuthority> authorities;

    private String username;

    private String password;

    private Boolean isAccountNonExpired;

    private Boolean isAccountNonLocked;

    private Boolean isCredentialsNonExpired;

    private Boolean isEnabled;

    /**
     * 通过userDetails对象，创建AuthUserDetailsFacade对象
     * @param userDetails 用户信息对象
     * @author red
     */
    public static AuthUserDetailsFacade getInstance(UserDetails userDetails){
        AuthUserDetailsFacade details=new AuthUserDetailsFacade();
        details.userDetails=JSONObject.parseObject(JSON.toJSONString(userDetails));
        details.authorities=userDetails.getAuthorities();
        details.username=userDetails.getUsername();
        details.password=userDetails.getPassword();
        details.isAccountNonExpired=userDetails.isAccountNonExpired();
        details.isAccountNonLocked=userDetails.isAccountNonLocked();
        details.isCredentialsNonExpired=userDetails.isCredentialsNonExpired();
        details.isEnabled=userDetails.isEnabled();
        return details;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return isAccountNonExpired == null || isAccountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return isAccountNonLocked == null || isAccountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return isCredentialsNonExpired == null || isCredentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return isEnabled == null || isEnabled;
    }


    /**
     * 通过实际userDetails对象的class类,获取userDetails对象
     * @param clazz 实际userDetails对象的class类
     * @author red
     */
    public <T> T getUserDetails(Class<T> clazz){
        return JSONObject.toJavaObject(userDetails,clazz);
    }

    /**
     * 通过实际userDetails对象的JSONObject对象
     * @author red
     */
    public JSONObject getUserDetails(){
        return userDetails;
    }

    public String getUserServiceId() {
        return userServiceId;
    }

    public void setUserServiceId(String userServiceId) {
        this.userServiceId = userServiceId;
    }
}